#coding=utf-8
import requests,json,urllib
import platform_login_api
import mysql_password
def reply(phone,content,num,commentId):
    try:
        s = requests.session()
        userid = mysql_password.password(phone)[1]  # 获取到userid
        #对回复进行评论
        password = mysql_password.password(phone)
        # 定义headers
        headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'ProcessorInfo': 'A11-6', 'AppVersion': '1.0.0',
                   'User-Agent': 'DuoYunCommunity/1.0.0 (iPhone; iOS 12.1.2; Scale/3.00)'
            , 'Uid': userid, 'Authorization': platform_login_api.login(phone)[2], 'Host': 'community-platform-api-test.gdgyun.com'
            , 'System': 'iOS', 'DeviceName': 'SmileSoul', 'AppName': 'DuoYunCommunity',
                   'SystemVersion': '12.1.2', 'Connection': 'keep-alive',
                   'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, hu-CN;q=0.7'
            , 'ScreenResolution': '750X1624'}
        for i in range(0, int(num), 1):
            reply_data = ({"commentId":str(commentId),"content":content+str(i),"userId":str(userid)})
            reply = s.post('http://community-article-api-test.gdgyun.com/api/community/article/comment/reply/add',json=reply_data,headers=headers)
            reply_id = str(reply.json()["data"]["id"])
        msg = reply.json()["msg"]
        #判断回复评论是否成功
        if str(msg) == '发表文章评论回复成功':
            #print str(msg)
            return str(msg) + u'，评论次数为：' + str(num) + u'次',reply_id
        else:
            print str(msg)
            return str(msg)
    except:
        return u'请填入相关信息后执行！'

def comment(phone,content,num,articleId):
    try:
        s = requests.session()
        userid = mysql_password.password(phone)[1]  # 获取到userid
        # 对文章进行评论
        # 定义headers
        headers1 = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'ProcessorInfo': 'A11-6', 'AppVersion': '1.0.0',
                   'User-Agent': 'DuoYunCommunity/1.0.0 (iPhone; iOS 12.1.2; Scale/3.00)'
            , 'Uid': userid, 'Authorization': platform_login_api.login(phone)[2],
                   'Host': 'community-platform-api-test.gdgyun.com'
            , 'System': 'iOS', 'DeviceName': 'SmileSoul', 'AppName': 'DuoYunCommunity',
                   'SystemVersion': '12.1.2', 'Connection': 'keep-alive',
                   'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, hu-CN;q=0.7'
            , 'ScreenResolution': '750X1624'}
        for i in range(0,int(num),1):
            comment_data = ({"content": content+str(i), "userId": str(userid), "articleId": str(articleId)})
            comment = s.post('http://community-article-api-test.gdgyun.com/api/community/article/comment/add',json=comment_data,headers=headers1)
        msg = comment.json()["msg"]
        commentId = comment.json()["data"]["id"]
        headers = platform_login_api.login(phone)[0]  # 获取到登录后的headers
        # coinsDetail = s.get('http://community-platform-api-test.gdgyun.com/api/community/reward/silver/coinsDetail?isIncoming=',headers=headers)  # 获取银币明细
        # sign_record = coinsDetail.json()["data"][0]["desc"]  # 获取desc字段值
        # coins = coinsDetail.json()["data"][0]["coins"]  # 获取银币数量
        # 判断发布文章评论是否成功
        if str(msg) == '发表文章评论成功':
            #print str(msg)+u'获得银币：'+str(coins)
            return str(msg)+u'，评论次数为：'+str(num)+u'次',commentId
        else:
            print str(msg)
            return str(msg)
    except:
        return u'请填入相关信息后执行！'

if __name__ == '__main__':
     # for i in range(0,10,1):
     #     reply('13810001009','回复评论0'+str(i))
    #comment('13810001009','测试评论测试00','10','528')
    reply('13810001009','测试评论测试00','1','2932')